package com.yuanlrc.hotel.home;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuanlrc.common.core.controller.BaseController;
import com.yuanlrc.common.core.domain.AjaxResult;
import com.yuanlrc.common.utils.security.Md5Utils;
import com.yuanlrc.hotel.RoomReserve.domain.RoomReserve;
import com.yuanlrc.hotel.RoomReserve.service.IRoomReserveService;
import com.yuanlrc.hotel.account.domain.Account;
import com.yuanlrc.hotel.account.service.IAccountService;
import com.yuanlrc.hotel.rate.domain.Rate;
import com.yuanlrc.hotel.rate.service.IRateService;
import com.yuanlrc.system.utils.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 *
 * 个人中心
 * @author wangzihan
 * @date : 2020/11/20 15:43
 */

@Controller
@RequestMapping("/home/index")
public class PersonalController extends BaseController {

    @Autowired
    private IAccountService accountService;

    @Autowired
    private IRoomReserveService roomReserveService;


    @Autowired
    private IRateService rateService;


    /**
     * 个人中心跳转，展示预约记录
     * @param model
     * @return
     */
    @RequestMapping("/personal_center")
    public String homeLogin(Model model,@RequestParam(defaultValue = "1") int pageNum) {
        Account logindeAccount = SessionUtil.getLogindeAccount();
        if (logindeAccount != null){
            PageHelper.startPage(pageNum,10);
            List<RoomReserve> roomReserves = roomReserveService.selectAllRoomReserve(logindeAccount.getId());
            PageInfo pageInfo = new PageInfo(roomReserves);
            model.addAttribute("pageInfo",pageInfo);
            model.addAttribute("roomReserves",roomReserves);
            model.addAttribute("logindeAccount",logindeAccount);

            return "home/index/personal_center";
        }else {
            return "home/login";
        }

    }


    /**
     * 修改保存前台用户
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Account account){
        /*如果修改个人信息时不修改头像，前端传过来的值是一个长度为0的非空字符串，
        这时如果不设置为null就会出现图片显示错误*/
        char[] chars = account.getHeadPic().toCharArray();
        if (chars.length==0){
            account.setHeadPic(null);
        }
        accountService.updateAccount(account);
        SessionUtil.set("account",account);
        return success();
    }

    /**
     *  前台用户修改密码
     * @param oldPassword  原密码
     * @param newPassword  新密码
     * @return
     */
    @PostMapping("/editPassword")
    @ResponseBody
    public AjaxResult editPassword(@RequestParam(value = "oldPassword",required = false)String oldPassword,
                                   @RequestParam(value = "newPassword",required = false)String newPassword){

        AjaxResult error = AjaxResult.error();
        Account logindeAccount = SessionUtil.getLogindeAccount();
        if (oldPassword == null || newPassword == null){
            error.put("code",600);
            return error;
        }
        //原密码md5加密后判断是否正确
        String oldHash = Md5Utils.hash(oldPassword);
        if (!oldHash.equals(logindeAccount.getPassword())){
            error.put("code",601);
            return error;
        }
        //原密码和新密码不能相同
        if (oldPassword.equals(newPassword)){
            error.put("code",602);
            return error;
        }
        //对新密码进行md5加密之后保存到数据库
        String newHash = Md5Utils.hash(newPassword);
        logindeAccount.setPassword(newHash);
        accountService.updateAccount(logindeAccount);
        //更新session信息
        SessionUtil.set("account",logindeAccount);

        return success();
    }


    /**
     * 评分
     */

    @RequestMapping(value = "rate_add", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult rateAdd(Rate rate, Integer itemid)
    {
        if(rate.getRoomId() == null)
        {
            return error("评分失败");
        }

        if(rate.getValue() == null)
        {
            return error("评分失败");
        }

        if(itemid == null)
        {
            return error("评分失败");
        }

        RoomReserve roomReserve = roomReserveService.selectRoomReserveById(itemid.longValue());
        if(roomReserve.getEndDateTime().compareTo(new Date()) > 0)
        {
            return error("请退房之后在评分");
        }


        if(rateService.insertRate(rate) == 0)
        {
            return error("评分失败");
        }

        roomReserve.setIsrate(RoomReserve.RATE);
        if(roomReserveService.updateRoomReserve(roomReserve) == 0)
        {
            return error("评分失败");
        }

        return toAjax(true);
    }

    @RequestMapping(value = "remove_reserve", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult removeReserve(RoomReserve roomReserve)
    {
        if(roomReserve == null)
        {
            return error("取消失败");
        }

        RoomReserve findById = roomReserveService.selectRoomReserveById(roomReserve.getId());

        Account account = SessionUtil.getLogindeAccount();
        if(account == null)
        {
            return error("请登录");
        }

        if(findById.getAccountId() != account.getId())
        {
            return error("你不可以取消别人的预约");
        }

        //可以不取消
        if(new Date().compareTo(findById.getStartDateTime()) > 0 )
        {
            return error("不可取消之前的预定信息");
        }

        if(roomReserveService.deleteRoomReserveById(findById.getId()) == 0)
        {
            return error("取消失败");
        }

        return success();
    }
}
